from apps.model import File, BankSideInfo, BankSideExtendInfo, BorrowerInfo, DiyaPerson
from apps.utils.static_file import delete_file


def delete_files_by_cid(cid):
    files = File.filter_by(history_id=cid).all()
    for file in files:
        delete_file(file.id)
        file.delete()


def delete_all_data(cid):
    bank: BankSideInfo = BankSideInfo.get(cid)
    bank.delete()
    extends = BankSideExtendInfo.filter_by(contract_id=bank.id).all()
    for extend in extends:
        extend.delete()
        BorrowerInfo.delete_all(BorrowerInfo.filter(BorrowerInfo.extend_id == extend.id).all())
        DiyaPerson.delete_all(DiyaPerson.filter(DiyaPerson.extend_id == extend.id).all())

        delete_files_by_cid(extend.id)
